package com.lottery.im.module.tim.model;

import android.content.Context;
import android.text.TextUtils;
import com.lottery.im.MyApplication;
import com.lottery.im.api.Api;
import com.tencent.imsdk.TIMGroupMemberInfo;
import com.tencent.imsdk.TIMGroupMemberRoleType;
import java.io.Serializable;

/**
 * 群成员数据
 */
public class GroupMemberProfile implements ProfileSummary, Serializable {

  private String avatarUrl = "";
  private String name;
  private String nameCard;
  private String nickName = "";
  private String id;
  private long quietTime;
  private TIMGroupMemberRoleType roleType;

  public GroupMemberProfile(TIMGroupMemberInfo info) {
    if (null == info)return;
    id = info.getUser();
    quietTime = info.getSilenceSeconds();
    roleType = info.getRole();

    if (!TextUtils.isEmpty(info.getNameCard())) {
      name = info.getNameCard();
      nameCard = info.getNameCard();
    }

    FriendProfile profile = FriendshipInfo.getInstance().getProfile(id);
    if (null != profile) {
      if (TextUtils.isEmpty(name)) {
        name = profile.getNickName();
      }
      avatarUrl = profile.getAvatarUrl();
      nickName = profile.getNickName();
    } else if ( id.equals(MyApplication.getInstance().getThirdAccId())) {
      if (TextUtils.isEmpty(name)) {
        name = MyApplication.getInstance().getUserInfo().getThirdUserName();
      }
      avatarUrl = Api.BASEADDRESS + MyApplication.getInstance().getUserInfo().getUserPic();
      nickName = MyApplication.getInstance().getUserInfo().getUserName();
    } else {
      if (TextUtils.isEmpty(name)) {
        name = info.getUser();
      }
    }
    if (TextUtils.isEmpty(nickName)){
      nickName = name;
    }
  }

  /**
   * 获取头像地址
   */
  @Override
  public String getAvatarUrl() {
    return avatarUrl;
  }

  /**
   * 获取名字 先群名片 后昵称
   */
  @Override
  public String getName() {
    if (!name.equals("")) {
      return name;
    }
    return id;
  }

  /**
   * 获取描述信息
   */
  @Override
  public String getDescription() {
    return null;
  }

  /**
   * 获取id
   */
  @Override
  public String getIdentify() {
    return id;
  }

  /**
   * 显示详情等点击事件
   *
   * @param context 上下文
   */
  @Override
  public void onClick(Context context) {

  }

  /**
   * 获取身份
   */
  public TIMGroupMemberRoleType getRole() {
    return roleType;
  }


  /**
   * 获取群名片
   */
  public String getNameCard() {
    if (nameCard == null) return "";
    return nameCard;
  }

  public long getQuietTime() {
    return quietTime;
  }

  public void setName(String name) {
    if (getNameCard().equals("")) {
      this.name = name;
    }
  }

  public void setId(String id) {
    this.id = id;
  }

  public void setQuietTime(long quietTime) {
    this.quietTime = quietTime;
  }

  public void setRoleType(TIMGroupMemberRoleType roleType) {
    this.roleType = roleType;
  }

  public void setAvatarUrl(String avatarUrl) {
    this.avatarUrl = avatarUrl;
  }

  public String getNickName() {
    return nickName;
  }

  public void setNickName(String nickName) {
    this.nickName = nickName;
  }
}
